﻿<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Bezdelnick
 * Date: 12.04.13
 * Time: 15:13
 * To change this template use File | Settings | File Templates.
 */
require_once("Config.php");
require_once("common/Lang.php");
require_once("persistence/Actor.php");
require_once("dao/ActorDao.php");

$actorDao = new ActorDao();
$actors = $actorDao->load();

if (isset($_POST['action']) && $_POST['action'] == "add") {
    if (isset($_POST['add_actor_first_name']) && isset($_POST['add_actor_last_name'])) {
        $createdActor = new Actor();
        $createdActor->setFirstName($_POST['add_actor_first_name']);
        $createdActor->setLastName($_POST['add_actor_last_name']);
        $actorDao->insert($createdActor);
    }
} else if (isset($_POST['action']) && $_POST['action'] == "edit") {
    if (isset($_POST['edit_actor_id']) && isset($_POST['edit_actor_first_name']) && isset($_POST['edit_actor_last_name'])) {
        $editedActor = new Actor();
        $editedActor->setId($_POST['edit_actor_id']);
        $editedActor->setFirstName($_POST['edit_actor_first_name']);
        $editedActor->setLastName($_POST['edit_actor_last_name']);
        $actorDao->update($editedActor);
    }
} else if (isset($_POST['action']) && $_POST['action'] == "remove") {
    if (isset($_POST['remove_actor_id'])) {
        $actorDao->delete($_POST['remove_actor_id']);
    }
}
?>

<!-- Content table -->
<table class="content-table" border="0">
    <thead style="background-color: #ffd700;">
    <tr>
        <td style="width: 10%;">ID</td>
        <td style="width: 30%;">First name</td>
        <td style="width: 35%;">Last name</td>
        <td style="width: 25%;">Operations</td>
    </tr>
    </thead>
    <tbody>
    <? foreach ($actors as $actor) { ?>
    <tr style="background-color: #efefef;">
        <td><?= $actor->getId() ?></td>
        <td><?= $actor->getFirstName() ?></td>
        <td><?= $actor->getLastName() ?></td>
        <td style="text-align: center;">
            <div class="jquery-button edit-actor-link"
                 itemId="<?= $actor->getId() ?>"><?= Lang::EDIT ?>
            </div>
            <div class="jquery-button remove-actor-link"
                 itemId="<?= $actor->getId() ?>"><?= Lang::REMOVE ?>
            </div>
        </td>
    </tr>
        <? } ?>
    </tbody>
</table>